package com.benio.ycfdisplayer;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.benio.ycfdisplayer.displayer.Options;
import com.benio.ycfdisplayer.displayer.YcfDisplayer;
import com.facebook.drawee.view.DraweeView;

public class ListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        YcfDisplayer.initialize(this);
        setContentView(R.layout.activity_list);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(new MyAdapter(DataUtil.IMAGES));
    }

    static class MyAdapter extends BaseAdapter {
        String[] mData;
        Options mOptions;

        public MyAdapter(String[] data) {
            mData = data;
            mOptions = new Options.Builder()
                    .setPlaceHolderImage(new ColorDrawable(Color.GREEN))
                    .setFailureImage(new ColorDrawable(Color.RED))
                    .build();
        }

        @Override
        public int getCount() {
            return mData.length;
        }

        @Override
        public Object getItem(int position) {
            return mData[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
            }
            DraweeView draweeView = (DraweeView) convertView;
            YcfDisplayer.get().display((String) getItem(position)).options(mOptions).into(draweeView);
            return convertView;
        }
    }
}
